home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Temperature / MainMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  5.0 KB  |  255 lines  |  [TEXT/CWIE]

  1. /* MainMenu.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <LowMem.h>
  10. #include <Menus.h>
  11. #include <TextEdit.h>
  12. #include <ToolUtils.h>
  13.  
  14. #include "Globals.h"
  15. #include "ResourceDefs.h"
  16. #include "Miscellany.h"
  17. #include "AMApp.h"
  18. #include "AMEngine.h"
  19. #include "AMWindow.h"
  20. #include "Dispatcher.h"
  21. #include "TemperatureDoc.h"
  22. #include "MainMenu.h"
  23.  
  24.  
  25. /*----------*/
  26. static long        GetCommandFromMenu (long        menuChoice);
  27. static void        DoApple            (short            itemNr);
  28. static void        Enable            (short            itemNr,
  29.                                  Boolean        enabled);
  30. static void        EnableTitle        (MenuHandle        menu,
  31.                                  Boolean        enabled);
  32.  
  33. /*----------*/
  34. void    InitTitles (void)
  35. {
  36. } /*InitTitles*/
  37.  
  38. /*----------*/
  39. void    LoadMenus (void)
  40. {
  41.     AppleMenu    = GetMenu (MENU_Apple);
  42.     FailNilResource ((Handle)AppleMenu);
  43.     AppendResMenu (AppleMenu, 'DRVR');
  44.     FileMenu    = GetMenu (MENU_File);
  45.     EditMenu    = GetMenu (MENU_Edit);
  46.  
  47.     InsertMenu (AppleMenu, 0);
  48.     InsertMenu (FileMenu, 0);
  49.     InsertMenu (EditMenu, 0);
  50.  
  51.     DrawMenuBar ();
  52. } /*LoadMenus*/
  53.  
  54. //----------
  55. long    GetCommandFromMenu (
  56.     long        menuChoice)
  57. {
  58.     long        commandID = 0;
  59.  
  60.     switch (menuChoice) {
  61.         case cAppleAbout:
  62.                 commandID = cmdInvokeAbout;
  63.             break;
  64.         case cFileNew:
  65.                 commandID = cmdNew;
  66.             break;
  67.         case cFileOpen:
  68.                 commandID = cmdOpen;
  69.             break;
  70.         case cFileClose:
  71.                 commandID = cmdClose;
  72.             break;
  73.         case cFileSave:
  74.                 commandID = cmdSave;
  75.             break;
  76.         case cFileSaveAs:
  77.                 commandID = cmdSaveAs;
  78.             break;
  79.         case cFileRevert:
  80.                 commandID = cmdRevert;
  81.             break;
  82.         case cFilePageSetup:
  83.                 commandID = cmdPageSetup;
  84.             break;
  85.         case cFilePrint:
  86.                 commandID = cmdPrint;
  87.             break;
  88.         case cFileQuit:
  89.                 commandID = cmdQuit;
  90.             break;
  91.         case cEditUndo:
  92.                 commandID = cmdUndo;
  93.             break;
  94.         case cEditCut:
  95.                 commandID = cmdCut;
  96.             break;
  97.         case cEditCopy:
  98.                 commandID = cmdCopy;
  99.             break;
  100.         case cEditPaste:
  101.                 commandID = cmdPaste;
  102.             break;
  103.         case cEditClear:
  104.                 commandID = cmdClear;
  105.             break;
  106.         case cEditSelectAll:
  107.                 commandID = cmdSelectAll;
  108.             break;
  109.         case cEditShowClipboard:
  110.                 commandID = cmdShowClipboard;
  111.             break;
  112.  
  113.         default:
  114.                 commandID = -menuChoice;
  115.     }
  116.  
  117.     return commandID;
  118. }
  119.  
  120. //----------
  121. void    DoApple (
  122.     short        itemNr)
  123. {
  124.     Str255            name;
  125.     short            refNum;
  126.  
  127.     GetMenuItemText (AppleMenu, itemNr, name);
  128.     refNum = OpenDeskAcc (name);
  129. }
  130.  
  131. /*----------*/
  132. void    DoMenu (
  133.     long        menuChoice)
  134. {
  135.     long            commandID;
  136.     AMDoc*            curDoc;
  137.     short            menuID;
  138.     short            itemNr;
  139.  
  140.     commandID = GetCommandFromMenu (menuChoice);
  141.     curDoc = cur->mDoc;
  142.     if (DoCommand (cur, commandID)) {    // Dispatch
  143.         // cur window handled it
  144.     } else if ((curDoc != nil)
  145.     && (DoDocCommand (curDoc, commandID))) {
  146.         // document handled it
  147.     } else if (DoAppCommand (gApplication, commandID)) {
  148.         // application handled it
  149.     } else {
  150.         menuID = HiWord (menuChoice);
  151.         itemNr = LoWord (menuChoice);
  152.         if (menuID == MENU_Apple) {
  153.             DoApple (itemNr);
  154.         }
  155.     }
  156.  
  157.     HiliteMenu (0);
  158. } /*DoMenu*/
  159.  
  160. /*----------*/
  161. MenuHandle        menu;
  162. Boolean            menuBarChanged;
  163.  
  164. /*----------*/
  165. static void Enable    (short        itemNr,
  166.                      Boolean    enabled)
  167. {
  168.     if (enabled) {
  169.         EnableItem  (menu, itemNr);
  170.     } else {
  171.         DisableItem (menu, itemNr);
  172.     }
  173. } /*Enable*/
  174.  
  175. /*----------*/
  176. static void EnableTitle    (MenuHandle        menu,
  177.                          Boolean        enabled)
  178. {
  179.     if (enabled != ((**menu).enableFlags & 1)) {
  180.         menuBarChanged = true;
  181.     }
  182.     if (enabled) {
  183.         EnableItem  (menu, 0);
  184.     } else {
  185.         DisableItem (menu, 0);
  186.     }
  187. } /*EnableTitle*/
  188.  
  189. /*----------*/
  190. void    UpdateMenus (void)
  191. {
  192.     WindowPeek        frontPeek;
  193.     Boolean            isFront;        /*is there a front window?*/
  194.     Boolean            isCur;            /*is there a current window?*/
  195.     Boolean            isCurDoc;        /*is there a current document?*/
  196.     Boolean            isDirty;        /*is it dirty?*/
  197.     Boolean            hasFile;        /*does it have a file?*/
  198.     Boolean            isSelected;        /*is anything selected?*/
  199.     Boolean            isDesk;            /*is the front window a desk acc?*/
  200.     Boolean            isText;            /*is there a current text field?*/
  201.     Boolean            isScrap;        /*is there any scrap?*/
  202.     TEHandle        curTE;
  203.  
  204.     menuBarChanged = false;
  205.  
  206.     isFront        = (FrontWindow () != nil);
  207.     isCur        = (curWindow != nil);
  208.     isDirty        = false;
  209.     hasFile        = false;
  210.     isSelected    = false;
  211.     isCurDoc    = (cur->mDoc != nil);
  212.     if (isCurDoc) {
  213.         isDirty        = AMEngine_IsDirty (cur->mDoc->mEngine);
  214.         hasFile        = AMEngine_HasFile (cur->mDoc->mEngine);
  215.     }
  216.  
  217.     isDesk = false;
  218.     if (isFront) {
  219.         frontPeek    = (WindowPeek) FrontWindow ();
  220.         isDesk        = (frontPeek->windowKind < 0);
  221.     }
  222.  
  223.     curTE = AMWindow_GetCurTE (cur);
  224.  
  225.     isText        = (curTE != nil);
  226.     isScrap        = false;
  227.     if (isText) {
  228.         isSelected    = ((**curTE).selStart != (**curTE).selEnd);
  229.         isScrap        = (TEGetScrapLength () > 0);
  230.     }
  231.  
  232.     menu = FileMenu;
  233.     Enable (cFileClose,        isFront);
  234.     Enable (cFileSave,        isDirty);
  235.     Enable (cFileSaveAs,    isCur);
  236.     Enable (cFileRevert,    isDirty);
  237.  
  238.     menu = EditMenu;
  239.     if (isFront) {
  240.         Enable (cEditUndo,        isDesk);
  241.         Enable (cEditCut,        isDesk || isSelected);
  242.         Enable (cEditCopy,        isDesk || isSelected);
  243.         Enable (cEditPaste,        isDesk || isScrap);
  244.         Enable (cEditClear,        isDesk || isSelected);
  245.         Enable (cEditSelectAll,    isText);
  246.  
  247.     }
  248.     EnableTitle (EditMenu,     isFront);
  249.  
  250.  
  251.     if (menuBarChanged) {
  252.         DrawMenuBar ();
  253.     }
  254. } /*UpdateMenus*/
  255.